package page418;

public class ThreadJoin implements Runnable {
    Thread joinThread1;
    Thread joinThread2;

    public void setThreadJoin1(Thread t1) {
        joinThread1 = t1;    //装运工
    }

    public void setThreadJoin2(Thread t2) {
        joinThread2 = t2;     //仓库管理员
    }

    public void run() {
        if (Thread.currentThread().getName().equals("货车司机")) {
            System.out.println(Thread.currentThread().getName() + "等待" + joinThread1.getName() + "搬运货物上车");
            try {
                Thread.sleep(3000);
                joinThread1.start();
                joinThread1.join();
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread().getName() + "拉走了10吨货物");
        } else if (Thread.currentThread() == joinThread1) {
            System.out.println(joinThread1.getName() + "等待仓库管理员开启仓库大门");
            try {
                Thread.sleep(4000);
                joinThread2.start();
                joinThread2.join();
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread().getName() + "搬了10吨货物上车");
        } else if (Thread.currentThread() == joinThread2) {
            System.out.println(joinThread2.getName() + "正在开启仓库大门，请等待");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread().getName() + "开启大门完毕");
        }
    }
}

